package com.java.clone;

import java.io.*;

public class Orange implements Serializable {

	private static final long serialVersionUID = 1L;

	private String color;
	private double price;
	private OrangeType type;
	private transient OrangeType type2;

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public OrangeType getType() {
		return type;
	}

	public void setType(OrangeType type) {
		this.type = type;
	}

	public OrangeType getType2() {
		return type2;
	}

	public void setType2(OrangeType type2) {
		this.type2 = type2;
	}

	/**
	 * 深复制：将对象序列化到内存，再从内存读取出来
	 * @return
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	public Orange depthClone() throws IOException, ClassNotFoundException {
		ByteArrayOutputStream byteout = new ByteArrayOutputStream();
		ObjectOutputStream out = new ObjectOutputStream(byteout);
		out.writeObject(this);
		ByteArrayInputStream bytein = new ByteArrayInputStream(byteout.toByteArray());
		ObjectInputStream in = new ObjectInputStream(bytein);
		return (Orange)in.readObject();

	}

	@Override
	public String toString() {
		return "Orange{" +
				"color=" + color +
				", price=" + price +
				", type=" + type.getType() +
				", type2=" + type2 +
				'}';
	}

}
